//
//  IMEIInputViewController.m
//  
//
//  Created by ツナミ 龍 on 15/12/16.
//
//

#import "IMEIInputViewController.h"
#import "CheckResultUserDefaults.h"
#import "ReactiveCocoa.h"

@implementation NSString (Space)

- (NSString *)removeAllSpaceCharacters
{
    NSString *string = [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    string = [string stringByReplacingOccurrencesOfString:@" " withString:@""];
    string = [string stringByReplacingOccurrencesOfString:@"　" withString:@""];
    return string;
}

@end

@implementation UITextField (Max)

- (void)setMaxLength:(NSInteger)maxLength
{
    RAC(self, text) = [[self.rac_textSignal map:^id(NSString *text) {
        return [text removeAllSpaceCharacters];
    }] map:^id(NSString *text) {
        return text.length <= maxLength ? text : [text substringToIndex:maxLength];
    }];
}

@end

@interface IMEIInputViewController ()

@property (weak, nonatomic) IBOutlet UITextField *imeiTextField;

@end

@implementation IMEIInputViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.imeiTextField setMaxLength:50];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    [CheckResultUserDefaults standardUserDefaults].imeiCodeString = self.imeiTextField.text;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end